;+
; NAME:
;  hod_readobs
; PURPOSE:
;  Reads in the observed w_p and errors, which are stored in the
;  COMMON block hod_data as rp_obs,wp_obs,wperr_obs
; CALLING SEQUENCE:
;  hod_readobs,col=col,lthresh=lthresh,cbin=cbin,/zehavi
; KEYWORDS:
;  col - 'blue' or 'red' or 'all'
;  lthresh - luminosity threshold given as M_B
;  cbin - color bin = blue_[1,2,3], green, red_[1,2]
; Must specify col and lthresh  OR  cbin
;  /zehavi - use the zehavi results
;-
@hod_common
pro hod_readobs,paper=paper,sample=sample
;pro hod_readobs,col=col,lthresh=lthresh,cbin=cbin,zehavi=zehavi,coil06=coil06
 COMMON hod_data
 
 if n_elements(paper) eq 0 or n_elements(sample) eq 0 then begin
     message,"Must define paper and sample"
 endif

 file=getenv("HOD_DIR")+"/dat/"+paper+"/"+sample+'.dat'
 if not file_test(file) then message,"Cannot find "+file

 readcol,file,comment='#',rp_obs,wp_obs,wperr_obs,format='F,F,F'

 ; now getting the redshift and space density constraint
 readcol,getenv("HOD_DIR")+"/dat/"+paper+"/"+paper+"_samples.dat",$
   comment='#',format='A,F,F,F,F,F',samp,maglim,zlo,zhi,zm,sd,sd_err

 w=where(samp eq sample,n)
 if n eq 0 then message,"Sample is not listed in "+paper+"_samples.dat"
 dens_obs=sd[w]
 denserr_obs=sd_err[w]
 meanz_obs=zm[w]

end


;   colflag=keyword_set(col)
;   lthreshflag=keyword_set(lthresh)
;   cbinflag=keyword_set(cbin)
;
;   if (colflag or lthreshflag) and cbinflag then $
;     message,"Specify col and lthresh OR  cbin"
;   if colflag xor lthreshflag then $
;     message,"for luminosity samples must specify col and lthresh"
;   if not colflag and not cbinflag then $
;     message,'Must specify col and lthresh OR  cbin'
;
;   if colflag then begin
;       ; reading the luminosity samples
;       if col ne 'blue' and col ne 'red' and col ne 'all' then $
;         message,"col must be blue or red or all"
;;       if lthresh ne -19.5 and lthresh ne -20 and $
;;         lthresh ne -20.5 and lthresh ne -21 then $
;;           message,"lthresh must be -19.5,-20,-20.5 or -21"
;       
;      if keyword_set(zehavi) then begin
;          ; reading Zehavi results from ASCII tables
;          format=strarr(19)+"X"
;          format[0]="F"
;          w=floor( (lthresh+22)*4)+1
;          format[w]="F"
;          format[w+1]="F"
;          format=strjoin(format,',')
;          readcol,getenv("HOD_DIR")+"/dat/zehavi/wp_rp_"+col+"_magthresh.dat",format=format,$
;                  rp_obs,wp_obs,wperr_obs
;       endif else if keyword_set(coil06) then begin
;
;          ;;;;;;;;;;;;;
;          ; Coil et al. 2006
;          file=getenv("HOD_DIR")+"/dat/Coil06/mean_error_wprp_corr_mb"+strtrim(string(-lthresh,format='(F6.1)'),2)+'.ascii'
;          readcol,file,format='F,X,X,F,F',comment='#',rp_obs,wp_obs,wperr_obs
;          ; reading density
;          readcol,getenv("HOD_DIR")+"/dat/Coil06/density.dat",format='A,F,F,F,F,F,F',samp,mlim,vega,cdens,cdenserr,tmdens,tmdenserr
;          q=where(strcmp(samp,col,/fold_case) gt 0 and mlim eq lthresh,n)
;          if n eq 0 then $
;             splog,"WARNING: no density information for ",col,lthresh $
;          else begin
;             dens_obs=tmdens[q]
;             denserr_obs=tmdenserr[q]
;          endelse
;          
;
;       endif else begin
;                 ; restoring data
;          restore,getenv("HOD_DIR")+'/dat/wprp_lum_results.sav'
;          rp_obs=lrbin[1:n_elements(lrbin)-1]
;          ex=execute('wp1=wp_'+col+'_'+string(-10*lthresh,format='(I3)'))
;          ex=execute('wpe=wp_'+col+'_'+string(-10*lthresh,format='(I3)')+'_err')
;          wp_obs=wp1[1:n_elements(lrbin)-1]
;          wperr_obs=wpe[1:n_elements(lrbin)-1]
;       endelse
;          
;       endif else if cbinflag then begin
;       if cbin ne 'blue_1' and cbin ne 'blue_2' and cbin ne 'blue_3' and $
;         cbin ne 'green' and cbin ne 'red_1' and cbin ne 'red_2' then $
;         message,"cbin must be blue_[1,2,3], green or red_[1,2]"
;
;       restore,getenv("HOD_DIR")+'/dat/wprp_color_results.sav'
;       rp_obs=lrbin[1:n_elements(lrbin)-1]
;       ex=execute('wp1=wp_'+cbin)
;       ex=execute('wpe=wp_'+cbin+'_err')
;       wp_obs=wp1[1:n_elements(lrbin)-1]
;       wperr_obs=wpe[1:n_elements(lrbin)-1]
;
;   endif
;
;end
